package com.mz.mattress.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.mz.mattress.api.entity.WxPayRecord;
import com.mz.mattress.client.mapper.WxPayRecordMapper;
import com.mz.mattress.client.service.WxPayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class WxPayRecordServiceImpl implements WxPayRecordService {

    @Autowired
    private WxPayRecordMapper wxPayRecordMapper;

    @Override
    public void insert(WxPayOrderNotifyResult result, String orderId) {

        LambdaQueryWrapper<WxPayRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxPayRecord::getOutTradeNo, result.getOutTradeNo());
        Long count = wxPayRecordMapper.selectCount(queryWrapper);
        if (count == 0) {
            WxPayRecord record = new WxPayRecord();
            record.setAppId(result.getAppid());
            record.setMchId(result.getMchId());
            record.setNonceStr(result.getNonceStr());
            record.setOpenid(result.getOpenid());
            record.setOutTradeNo(result.getOutTradeNo());
            record.setResultCode(result.getResultCode());
            record.setReturnCode(result.getReturnCode());
            record.setSign(result.getSign());
            record.setTimeEnd(result.getTimeEnd());
            record.setTotalFee(result.getTotalFee());
            record.setTradeType(result.getTradeType());
            record.setTransactionId(result.getTransactionId());
            record.setOrderId(orderId);
            wxPayRecordMapper.insert(record);
        }
    }

    @Override
    public void insert(WxPayOrderQueryResult result, String orderId) {
        LambdaQueryWrapper<WxPayRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxPayRecord::getOutTradeNo, result.getOutTradeNo());
        Long count = wxPayRecordMapper.selectCount(queryWrapper);
        if (count == 0) {
            WxPayRecord record = new WxPayRecord();
            record.setAppId(result.getAppid());
            record.setMchId(result.getMchId());
            record.setNonceStr(result.getNonceStr());
            record.setOpenid(result.getOpenid());
            record.setOutTradeNo(result.getOutTradeNo());
            record.setResultCode(result.getResultCode());
            record.setReturnCode(result.getReturnCode());
            record.setSign(result.getSign());
            record.setTimeEnd(result.getTimeEnd());
            record.setTotalFee(result.getTotalFee());
            record.setTradeType(result.getTradeType());
            record.setTransactionId(result.getTransactionId());
            record.setOrderId(orderId);
            wxPayRecordMapper.insert(record);
        }
    }

    @Override
    public List<WxPayRecord> selectByOrderId(Serializable orderId) {

        LambdaQueryWrapper<WxPayRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxPayRecord::getOrderId, orderId);
        List<WxPayRecord> list = wxPayRecordMapper.selectList(queryWrapper);

        return list;
    }
}
